package com.bkhech.spring.security.example.util;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;

/**
 * Servlet相关工具类封装扩展
 *
 * @author guowm
 * @date 2023/1/11
 */
public class ServletUtils extends ServletUtil {

    /**
     * 返回 JSON 字符串
     * <p>
     * 必须使用 APPLICATION_JSON_UTF8_VALUE，否则会乱码
     *
     * @param response 响应
     * @param object   对象，会序列化成 JSON 字符串
     */
    @SuppressWarnings("deprecation")
    public static void writeJSON(HttpServletResponse response, Object object) {
        String content = JSONUtil.toJsonStr(object);
        ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE);
    }

}
